﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InitMaze : MonoBehaviour
{
    public GameObject cubPrefab;
    public Transform planeDown;
    [SerializeField]
    private int hTempCount;
    [SerializeField]
    private int vTempCount;

    public int iSide = 10;
    public int iMiss = 5;
    [SerializeField]
    private List<GameObject> goList;
    [SerializeField]
    private List<GameObject> goSideList;
    [SerializeField]
    private GameObject goZero;
    private int goOut;
    public BoxCollider endBoxCollider;

    public GameObject goNearDoor;


    public void BuildHome(int hCount, int vCount)
    {
        hTempCount = hCount;
        vTempCount = vCount;

        for (int i = 0; i < hCount; i++)
        {
            for (int j = 0; j < vCount; j++)
            {
                GameObject goTemp = Instantiate(cubPrefab, new Vector3(i * 10, iSide / 2, j * 10), Quaternion.identity);
                if (i == 1 && j == 1)
                {
                    goZero = goTemp;
                }
                if (i == 0 || i == hCount - 1 || j == 0 || j == vCount - 1)
                {
                    goSideList.Add(goTemp);
                }
                else
                {
                    goList.Add(goTemp);
                }
            }
        }
        BuildMoze();
        InitPanels(hCount, vCount);
    }
    void BuildMoze()
    {
        int iOut = Random.Range(1, goSideList.Count);
        //goSideList[iOut].SetActive(false);
        //出口位置。保留出口位置的boxcollider
        goSideList[iOut].GetComponent<MeshRenderer>().enabled = false;
        endBoxCollider = goSideList[iOut].GetComponent<BoxCollider>();
        endBoxCollider.isTrigger = true;
        goOut = iOut;

        for (int i = 0; i < goList.Count; i++)
        {
            int j = Random.Range(1, iMiss);

            if (j == 1)
            {
                goList[i].SetActive(true);
            }
            else
            {
                goList[i].SetActive(false);
            }
        }
        if (goZero.activeSelf)
        {
            goZero.SetActive(false);
        }

        CheckMoze();
    }
    void InitPanels(int hCount, int vCount)
    {
        Transform goPanel = Instantiate(planeDown, new Vector3(0, 0, 0), Quaternion.identity);

        float hPostion = ((float)hCount) * iSide / 2 - 5;
        float vPostion = ((float)vCount) * iSide / 2 - 5;
        float hScale = (float)hCount;
        float vScale = (float)vCount;
        goPanel.position = new Vector3(hPostion, 0, vPostion);
        goPanel.localScale = new Vector3(hScale, planeDown.localScale.y, vScale);
    }
    /// <summary>
    ///  检查是否是死局，并修正
    /// </summary>
    void CheckMoze()
    {
        //和出口相邻的位置一定要为空
        Transform goDoor = goSideList[goOut].transform;
        int goNearDoorX = 0;
        int goNearDoorZ = 0;
        if (goDoor.position.x == 0)
        {
            //Z轴起始边
            goNearDoorX = iSide;
            goNearDoorZ = (int)goDoor.position.z;
        }
        else if (goDoor.position.x == (hTempCount * iSide - iSide))
        {
            //Z轴末尾边
            goNearDoorX = hTempCount * iSide - iSide * 2;
            goNearDoorZ = (int)goDoor.position.z;

        }
        else if (goDoor.position.z == 0)
        {
            //x轴起始边
            goNearDoorX = (int)goDoor.position.x;
            goNearDoorZ = iSide;
        }
        else if (goDoor.position.z == (vTempCount * iSide - iSide))
        {
            //x轴末尾边
            goNearDoorX = (int)goDoor.position.x;
            goNearDoorZ = vTempCount * iSide - iSide * 2;
        }
        foreach (GameObject item in goList)
        {
            if (item.transform.position.x == goNearDoorX && item.transform.position.z == goNearDoorZ)
            {
                goNearDoor = item;
                if (!item.activeSelf)
                {
                    item.SetActive(false);
                }
            }
        }

        Debug.Log("~~~~~~~~~~~~~~~~~~~~~~~~");
        //其余有无死胡同
        for (int i = 0; i < goList.Count - 1; i++)
        {
            if (!goList[i])
            {
                Transform goTransform = goList[i].transform;
                GameObject goFrount = null; //前（z轴不变，x+1）
                GameObject goBehind = null;//后（z轴不变，x-1）
                GameObject goLift = null;//左（x轴不变，z+1）
                GameObject goRight = null;//右（x轴不变，z-1）

                if (goTransform.position.z == iSide)
                {
                    //x起始第二排.不检查右边
                    goRight = null;

                    if (i + vTempCount - 2 > goList.Count)
                    {
                        goFrount = null;
                    }
                    else
                    {
                        goFrount = goList[i + vTempCount - 2];
                    }
                    if (i - vTempCount - 2 < 0)
                    {
                        goBehind = null;
                    }
                    else
                    {
                        goBehind = goList[i - vTempCount - 2];
                    }
                    goLift = goList[i + 1];
                    SetGOFalse(goFrount, goBehind, goLift);
                }
                else if (goTransform.position.x == iSide)
                {
                    //z起始第二排,不检查后边
                    goBehind = null;
                    goFrount = goList[i + vTempCount - 2];
                    if (goList[i + 1].transform.position.z == (vTempCount - 1) * iSide)
                    {
                        goLift = null;
                    }
                    else
                    {
                        goLift = goList[i = 1];
                    }

                    if (goList[i - 1].transform.position.z == 0)
                    {
                        goRight = null;
                    }
                    else
                    {
                        goRight = goList[i - 1];
                    }
                    SetGOFalse(goFrount, goRight, goLift);
                }
                else if (goTransform.position.z == (vTempCount * iSide - iSide * 2))
                {
                    //x倒数第二排.不计算左边
                    goLift = null;
                    goRight = goList[i - 1];
                    if (i + vTempCount - 2 > goList.Count)
                    {
                        goFrount = null;
                    }
                    else
                    {
                        goFrount = goList[i + vTempCount - 2];
                    }
                    if (i - vTempCount - 2 < 0)
                    {
                        goBehind = null;
                    }
                    else
                    {
                        goBehind = goList[i - vTempCount - 2];
                    }
                    SetGOFalse(goRight, goBehind, goLift);
                }
                else if (goTransform.position.x == (hTempCount * iSide - iSide * 2))
                {
                    //z倒数第二排。不计算前面
                    goFrount = null;
                    goBehind = goList[i - vTempCount - 2];

                    if (goList[i + 1].transform.position.z == (vTempCount - 1) * iSide)
                    {
                        goLift = null;
                    }
                    else
                    {
                        goLift = goList[i = 1];
                    }

                    if (goList[i - 1].transform.position.z == 0)
                    {
                        goRight = null;
                    }
                    else
                    {
                        goRight = goList[i - 1];
                    }
                    SetGOFalse(goBehind, goRight, goLift);
                }
                else
                {
                    goFrount = goList[i + vTempCount - 2];
                    goBehind = goList[i - vTempCount - 2];
                    goLift = goList[i + 1];
                    goRight = goList[i - 1];
                    SetGOFalse(goRight, goLift, goFrount, goBehind);
                }
            }
        }
    }

    void SetGOFalse(GameObject go1, GameObject go2, GameObject go3)
    {
        if (go1.activeSelf && go2.activeSelf && go3.activeSelf )
        {
            int iTemp = Random.Range(1, 5);
            Debug.Log("三边都是墙" + iTemp);

            switch (iTemp)
            {
                case 1:
                    go1.SetActive(false);
                    break;
                case 2:
                    go2.SetActive(false);
                    break;
                case 3:
                    go3.SetActive(false);
                    break;
            }
        }
    }
    void SetGOFalse(GameObject go1, GameObject go2, GameObject go3,GameObject go4)
    {
        if (go1.activeSelf && go2.activeSelf && go3.activeSelf && go4.activeSelf)
        {
            int iTemp = Random.Range(1, 5);
            Debug.Log("四边边都是墙" + iTemp);

            switch (iTemp)
            {
                case 1:
                    go1.SetActive(false);
                    break;
                case 2:
                    go2.SetActive(false);
                    break;
                case 3:
                    go3.SetActive(false);
                    break;
                case 4:
                    go4.SetActive(false);
                    break;
            }
        }
    }
}
